package org.zhq.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhq.ServerResult;
import org.zhq.pojo.UserAddress;
import org.zhq.pojo.bo.AddressBO;
import org.zhq.service.AddressService;

import java.util.List;

@Api(value = "收获地址接口",tags = {"收获地址相关接口"})
@RestController
@RequestMapping("address")
@Slf4j
public class AddressController {

//    用户在确认订单页面，可以针对收货地址做如下操作:
//    1、查询用户的所有收货地址列表
//    2、新增收货地址
//    3、删除收货地址
//    4、修改收货地址
//    5、设置默认收货地址

    private final AddressService addressService;

    @Autowired
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @ApiOperation(value = "根据用户id查询收货地址列表",notes = "根据用户id查询收货地址列表",httpMethod = "POST")
    @PostMapping("/list")
    public ServerResult list(@RequestParam String userId){
        if(StrUtil.isBlank(userId)){
            return ServerResult.errorMsg(null);
        }
        List<UserAddress> list = addressService.queryAll(userId);
        return ServerResult.ok(list);
    }

    @ApiOperation(value = "用户新增地址",notes = "用户新增地址",httpMethod = "POST")
    @PostMapping("/add")
    public ServerResult add(@RequestBody AddressBO addressBO){
        ServerResult serverResult = checkAddress(addressBO);
        if(!serverResult.isOK()){
            return serverResult;
        }
        addressService.addNewUserAddress(addressBO);
        return ServerResult.ok();
    }

    @ApiOperation(value = "用户修改地址",notes = "用户修改地址",httpMethod = "POST")
    @PostMapping("/update")
    public ServerResult update(@RequestBody AddressBO addressBO){
        String addressId = addressBO.getAddressId();
        if(StrUtil.isBlank(addressId)){
            ServerResult.errorMsg("修改地址错误");
        }
        ServerResult serverResult = checkAddress(addressBO);
        if(!serverResult.isOK()){
            return serverResult;
        }
        addressService.updateUserAddress(addressBO);
        return ServerResult.ok();
    }

    @ApiOperation(value = "用户删除地址",notes = "用户删除地址",httpMethod = "POST")
    @PostMapping("/delete")
    public ServerResult delete(@RequestParam String userId,
                               @RequestParam String addressId){
        if(StrUtil.isBlank(addressId) || StrUtil.isBlank(userId)){
            ServerResult.errorMsg("删除地址错误");
        }
        addressService.deleteUserAddress(userId,addressId);
        return ServerResult.ok();
    }

    @ApiOperation(value = "用户设置默认地址",notes = "用户设置默认地址",httpMethod = "POST")
    @PostMapping("/setDefault")
    public ServerResult setDefault(@RequestParam String userId,
                               @RequestParam String addressId){
        if(StrUtil.isBlank(addressId) || StrUtil.isBlank(userId)){
            ServerResult.errorMsg("设置默认地址错误");
        }
        addressService.updateUserAddressToBeDefault(userId,addressId);
        return ServerResult.ok();
    }

    private ServerResult checkAddress(AddressBO addressBO){
        String receiver = addressBO.getReceiver();
        if(StrUtil.isBlank(receiver)){
            return ServerResult.errorMsg("收货人不能为空");
        }
        if(receiver.length()>12){
            return ServerResult.errorMsg("收货人姓名不能太长");
        }
        String mobile = addressBO.getMobile();
        if(StrUtil.isBlank(mobile)){
            return ServerResult.errorMsg("收货人手机号不能为空");
        }
        if(mobile.length()!=11){
            return ServerResult.errorMsg("收货人手机号长度不正确");
        }
        if(!Validator.isMobile(mobile)){
            return ServerResult.errorMsg("收货人手机号格式不正确");
        }
        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if(StrUtil.isBlank(province)||
            StrUtil.isBlank(city)||
            StrUtil.isBlank(district)||
            StrUtil.isBlank(detail)){
            return ServerResult.errorMsg("收货地址信息不能为空");
        }
        return ServerResult.ok();
    }

}
